package com.marvels.approve.loan.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.marvels.approve.common.dto.InsurancePolicyDTO;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.loan.service.McamLoanBxCompanyService;
import com.marvels.approve.loan.service.McamLoanInsurancePolicyInfoService;
import com.marvels.common.dto.MarvelsApiResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
/**
 * 保单相关
 * @author zhaosa
 *
 */
@RequestMapping("/approve/bdrelation/api")
@RestController
public class InsuranceRelationApiController {
	
	@Autowired
	private McamLoanInsurancePolicyInfoService mcamLoanInsurancePolicyInfoService;
	@Autowired
	private McamLoanBxCompanyService mcamLoanBxCompanyService;
	@Value("${spring.application.name}")
	private String appName;
	/**
	 * 获取保单信息
	 * @param applyInfoDto
	 * @return
	 */
	@RequestMapping("/getBdInfo")
	@ResponseBody
	public MarvelsApiResultDTO getBdInfoList(@RequestBody InsurancePolicyDTO applyInfoDto){
		List<InsurancePolicyDTO> loanBdInfoList = null;
		try {
			loanBdInfoList = mcamLoanInsurancePolicyInfoService.getBdInfoList(applyInfoDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================同步保单给老核心失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================同步保单给老核心异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),loanBdInfoList,appName,"");
	}
	/**
	 * 生成保单信息
	 * <p>目前主要适用于祁安保险<p>
	 * @param applyInfoDto
	 * @return
	 */
	@RequestMapping("/generateBdInfo")
	@ResponseBody
	public MarvelsApiResultDTO generateBdInfo(@RequestBody McamLoanApplyInfoDTO applyInfoDto){
		Integer count = null;
		try {
			count = mcamLoanInsurancePolicyInfoService.generateBdInfo(applyInfoDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================生成保单失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================生成保单异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),count,appName,"");
	}
	/**
	 * 修改保单密码
	 * @param insurancePolicyDto
	 * @return
	 */
	@RequestMapping("/modifyBdPassword")
	@ResponseBody
	public MarvelsApiResultDTO modifyBdPassword(@RequestBody InsurancePolicyDTO insurancePolicyDto){
		Integer count = null;
		try {
			count = mcamLoanInsurancePolicyInfoService.modifyBdPassword(insurancePolicyDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================修改保单密码失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================修改保单密码异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),count,appName,"");
	}
	/**
	 * 获取保险公司列表
	 * @return  annualPremiumRate
	 */
	@RequestMapping("/selectBxCompanyList")
	@ResponseBody
	public MarvelsApiResultDTO selectBxCompanyList(){
		List<Map<String,Object>> result = null;
		try {
			result = mcamLoanBxCompanyService.selectBxCompanyListOuter();
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("===================获取保险公司列表失败："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null,appName,"");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("===================获取保险公司列表异常："+e);
			return MarvelsApiResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), null,appName,"");
		}
		return MarvelsApiResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),result,appName,"");
	}
}
